//package com.ftt;
//  import javax.servlet.ServletException;
//  import javax.servlet.annotation.WebServlet;
//  import javax.servlet.http.HttpServlet;
//  import javax.servlet.http.HttpServletRequest;
//  import javax.servlet.http.HttpServletResponse;
//  import javax.servlet.http.HttpSession;
//  import java.io.IOException;
//  import java.sql.Connection;
//  import java.sql.PreparedStatement;
//  import java.sql.ResultSet;
//  import java.sql.SQLException;
//
//@WebServlet("/login.do")
//public class LoginServlet extends HttpServlet{
//
//      @Override
//      protected void doGet(HttpServletRequest req, HttpServletResponse resp)
//              throws ServletException, IOException {
//          // 处理GET请求
//          System.out.println("doGet");
//          req.getRequestDispatcher("index.jsp").forward(req, resp);
//      }
//
//      @Override
//      protected void doPost(HttpServletRequest req, HttpServletResponse resp)
//              throws ServletException, IOException {
//          String username = req.getParameter("username");
//          String password = req.getParameter("password");
//          if ("fangtaotao".equals(username) && "123456".equals(password)) {
//              req.getRequestDispatcher("首页.jsp").forward(req, resp);
//          }else {
//              req.getRequestDispatcher("index.jsp").forward(req, resp);
//          }
//      }
//}



package com.ftt;

import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;

//@WebServlet("/login.do")
public class LoginServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // 处理GET请求
        System.out.println("doGet");
        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sql = "select * from tb_manager where name=? and password=?";
        //1、获取连接
        Connection conn = DBUtile.getConnection();
        //2、获取操作对象
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery();
            if(rs.next()){
                req.getRequestDispatcher("product_list.jsp").forward(req, resp);
            }else{
                req.setAttribute("zhanghao", "账号或密码错误");
                req.getRequestDispatcher("登录.jsp").forward(req, resp);
            }
        }catch (SQLException e) {

            throw new RuntimeException(e);
        }
        //3、关闭资源
        DBUtile.closeResource(conn, ps, rs);
    }
}

